/**
 @Name：管理员管理
 @Author：cike
 @Date：2019-5-26
**/

layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,setter = layui.setter
  ,form = layui.form;

  // table设置
  table.set({
    where: {
      access_token: layui.data(setter.tableName)[setter.request.tokenName]
    }
    ,method: 'post'
    ,response:{
      statusCode: 200
    }
    ,request:{
      pageName: "page"
      ,limitName: "size"
    }
    ,page: true
    ,text: { none : "暂无相关数据" }
    ,done: function (res) {
      if(res[setter.response.statusCode] == setter.response.statusCode.logout){
        view.exit();
      }
    }
    ,parseData: function(res){ //res 即为原始返回的数据
      return {
        "code": res.code, //解析接口状态
        "msg": res.msg, //解析提示文本
        "count": res.data.total, //解析数据长度
        "data": res.data.list //解析数据列表
      };
    }
  });

  //管理员管理
  table.render({
    elem: '#back-admin-list'
    ,url: setter.api_back_admin_list
    ,cols: [[
      {field: 'id', width: 65, title: 'ID', sort: true}
      ,{field: 'name', width: 100, title: '登录名'}
      ,{field: 'role', width: 100, title: '角色'}
      ,{field: 'status', width: 100, title:'状态', templet: '#buttonSwitch', minWidth: 80, align: 'center'}
      ,{field: 'last_login_time', width: 160, title: '最后登录时间'}
      ,{field: 'update_time', title: '更新时间'}
      ,{field: 'create_time', title: '创建时间'}
      ,{title: '操作', width: 150, align: 'center', toolbar: '#back-admin-list-operation'}
    ]]

    
  });
  
  //监听工具条
  table.on('tool(back-admin-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此管理员？', function(index){
        admin.req({
          url: setter.api_back_admin_delete
          ,data: {id:obj.data.id}
          ,done: function(res){
            layer.msg(res.msg);
            layer.close(index); //执行关闭 
            table.reload('back-admin-list'); //重载表格
          }
        });
      });
    }else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑管理员'
        ,area: admin.screen() < 2 ? ['100%','400px'] : ['450px','400px']
        ,id: 'back-admin-edit-popup'
        ,success: function(layero, index){
          view(this.id).render('administrator/form', data).done(function(){
            // 角色下拉框
            admin.req({
              url: setter.api_back_role_list
              ,data: {status:1}
              ,done: function(res){
                $.each(res.data.list, function (index, item) {
                  is_checked = item.id == data.role_id?'checked':'';
                  $('#back-admin-form-role-select').append(new Option(item.name, item.id, is_checked, is_checked));// 下拉菜单里添加元素
                });
                form.render(null, 'back-admin-form');
              }
            });
            //监听提交
            form.on('submit(back-admin-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              field.status = field.status?1:0;
              admin.req({
                url: setter.api_back_admin_update
                ,data: field
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭 
                  table.reload('back-admin-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });
    }
  });

  exports('administrator', {})
});